package com.gasolinator.a.app.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.apache.commons.lang3.StringEscapeUtils;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by IntelliJ IDEA.
 * User: fjhidalgo
 * Date: 11/12/11
 * Time: 19:55
 * To change this template use File | Settings | File Templates.
 */
public class GasolinatorDBSearcher {

    private static SQLiteDatabase db;
    static {

    }

    public static String get(Context context, double lat,double lon, float dist) {

        StringBuilder builder = new StringBuilder();
        builder.append("[");
        int debugcount = 500;
        int count = 0;

        try {

            String sqlQuery = "SELECT field2 from flech WHERE " +
                    "((((field3 - " + lat + ") * (field3 - " + lat + ")) + (field4 - (" + lon + ")) * (field4 - (" + lon + "))) * (75 * 75) <= " + dist + ")=1" +
                    " LIMIT "+debugcount;
            Log.i("gasolinator DB Query ====:","["+sqlQuery+"]");



            Cursor cursor = getCursorFromSql(context,sqlQuery);

            if (cursor != null) {
                try {
                    if (cursor.getCount()<1)
                    {
                        cursor.close();
                        db.close();
                        Log.i("gasolinator DB Query ( cursor.getCount()<1 ) ====:","["+sqlQuery+"]");
                        return "[]";
                    }
                    while (!cursor.isLast()) {
                        cursor.moveToNext();
                        builder.append(cursor.getString(0).toString());
                        if (!cursor.isLast()) {
                            builder.append(",");
                        }
                        count++;
                    }
                } finally {
                    cursor.close();
                }
            }
        } finally {
            if (db != null) db.close();
        }
        Log.i("DB Gasolinator ====:", count +" rows founded.");

        builder.append("]");
        final String resultString = builder.toString();
        return StringEscapeUtils.unescapeHtml3(resultString);

	}

    private static Cursor getCursorFromSql(Context context, String sqlQuery)
    {
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        try {
            dbHelper.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
        db = dbHelper.openDataBase();
        return db.rawQuery(sqlQuery,null);
    }

    public static Map<Integer,String> getGasStations(Context context)
    {
        Map<Integer,String> gasStations = new ConcurrentHashMap<Integer,String>();

         int count = 0;

        try {

            String sqlQuery = "SELECT field1,field5 from flech";
            Log.i("gasolinator DB Query 2 ====:","["+sqlQuery+"]");

            Cursor cursor = getCursorFromSql(context,sqlQuery);

            if (cursor != null) {
                try {
                    if (cursor.getCount()<1)
                    {
                        cursor.close();
                        db.close();
                        Log.i("gasolinator DB Query ( cursor.getCount()<1 ) ====:","["+sqlQuery+"]");
                        return null;
                    }
                    while (!cursor.isLast()) {
                        cursor.moveToNext();
                        gasStations.put(cursor.getInt(0),cursor.getString(1));
                        Log.i("DB Gasolinator ====:", count +"  => ID:["+cursor.getInt(0)+"] GEO: ["+cursor.getString(1)+"]" );
                        count++;
                    }
                } finally {
                    cursor.close();
                }
            }
        } finally {
            if (db != null) db.close();
        }

        Log.i("DB Gasolinator ====:", count +" rows founded.");
        return gasStations;

    }
}
